package com.my.controller;


import com.alibaba.fastjson.JSONObject;
import com.my.bean.FoodRelation;
import com.my.bean.ResultPage;
import com.my.bean.VoPage;
import com.my.service.FoodRelationService;
import com.my.util.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

/**
 * <p>
 * 月子餐单 前端控制器
 * </p>
 *
 * @author 闫亚茹
 * @since 2018-09-09
 */
@Controller
@RequestMapping("/foodRelation")
public class FoodRelationController {
@Autowired
FoodRelationService foodRelationService;


    @RequestMapping("selectFoodRelation")
    public String selectFoodRelation(){
        return  "foot";
    }

    @RequestMapping("/foot_Add")
    public String footAdd(){
        return  "/Alert/foot_Add";
    }

    @RequestMapping("/foot_Edit")
    public String footEdit(String homeId,Model model){
        model.addAttribute("homeId",homeId);
        return  "/Alert/foot_Edit";
    }

//根据房间ID查询 该房间关联的月子餐
    @RequestMapping("/findFoodData")
    @ResponseBody
    public ResultPage<FoodRelation> findFoodData(VoPage voPage, @RequestParam("homeId") String homeId, HttpSession session){
        boolean admin=false;
        //如果事超级管理员  即可查看所有的房间  判断是否为超级管理员
        if(SessionUtil.getSessionUser(session).getRoles().getRoleValue().equals("9f77d5a8-75c6-4d06-adf9-a37e727d491a")){
            admin=true;
        }
        return  foodRelationService.selectMyPage(voPage,homeId);
    }
    //给某个房间 添加菜品
    @RequestMapping("/insertFood")
    @ResponseBody
    public String insertFood(String param){
        FoodRelation foodRelation=JSONObject.parseObject(param,FoodRelation.class);
        foodRelation.setIsDelete(2);
        if (foodRelationService.insert(foodRelation)){
            return "保存成功";
        }else{
            return  "保存失败";
        }

    }
    //给某个房间 修改菜品
    @RequestMapping("/updateFood")
    @ResponseBody
    public String updateFood(String param){
        FoodRelation foodRelation=JSONObject.parseObject(param,FoodRelation.class);
        if (foodRelationService.updateById(foodRelation)){
            return "修改成功";
        }else{
            return  "修改失败";
        }

    }
}
